package cz.via.facepalm.calendar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.ViewFlipper;

public class ActivitiesActivity extends Activity 
{
	CalendarTask[] tasks;
	ViewFlipper flipper;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activities);

		tasks = new CalendarTask[2];
		tasks[0] = new CalendarTask("Via deadline", new String[] { "7:30", "1",
				"12" }, "FacePalm", "KN:E-301", "Seminar", "VIA project");
		tasks[1] = new CalendarTask("PSI seminar", new String[] { "10:00", "5",
				"12" }, "FacePalm", "KN:E-107", "toaleta", "hygiena");

		flipper = (ViewFlipper) findViewById(R.id.flipper);
		
		Button prevBtn = (Button) findViewById(R.id.prev_btn);
		prevBtn.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				System.out.println("Prev clicked");
				flipper.setInAnimation(inFromLeftAnimation());
				flipper.setOutAnimation(outToRightAnimation());
				flipper.showNext();							
			}
		}
		);
		
		Button nextBtn = (Button) findViewById(R.id.next_btn);
		nextBtn.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				System.out.println("Next clicked");
				flipper.setInAnimation(inFromRightAnimation());
				flipper.setOutAnimation(outToLeftAnimation());
				flipper.showPrevious();
			}
		}
		);
		
		SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
				this, createGroupList(), R.layout.group_row, new String[] {
						"Hour", "Date", "Title" }, new int[] { R.id.row_hour,
						R.id.row_date, R.id.row_title }, createChildList(),
				R.layout.child_row, new String[] { "Subname", "Subvalue" },
				new int[] { R.id.grp_name, R.id.grp_value });

		ExpandableListView v = (ExpandableListView) findViewById(R.id.exp);
		v.setAdapter(expListAdapter);
	}

	private Animation inFromRightAnimation() {
		Animation inFromRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(500);
		inFromRight.setInterpolator(new AccelerateInterpolator());
		return inFromRight;
	}

	private Animation outToLeftAnimation() {
		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(500);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}

	private Animation inFromLeftAnimation() {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(500);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}

	private Animation outToRightAnimation() {
		Animation outtoRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(500);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}

	@SuppressWarnings("unchecked")
	private List createGroupList() {
		ArrayList result = new ArrayList();
		for (int i = 0; i < tasks.length; ++i) {
			HashMap m = new HashMap();
			m.put("Date", tasks[i].date[1] + '.' + tasks[0].date[2] + '.');
			m.put("Hour", tasks[i].date[0]);
			m.put("Title", tasks[i].title);
			result.add(m);
		}
		return (List) result;
	}

	@SuppressWarnings("unchecked")
	private List createChildList() {
		ArrayList result = new ArrayList();
		for (int i = 0; i < tasks.length; ++i) {
			ArrayList secList = new ArrayList();

			HashMap child1 = new HashMap();
			child1.put("Subname", "Group:");
			child1.put("Subvalue", tasks[i].group);
			secList.add(child1);

			HashMap child2 = new HashMap();
			child2.put("Subname", "Place:");
			child2.put("Subvalue", tasks[i].place);
			secList.add(child2);

			HashMap child3 = new HashMap();
			child3.put("Subname", "Type:");
			child3.put("Subvalue", tasks[i].type);
			secList.add(child3);

			HashMap child4 = new HashMap();
			child4.put("Subname", "Project:");
			child4.put("Subvalue", tasks[i].project);
			secList.add(child4);

			result.add(secList);
		}
		return (List) result;
	}
}